package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.event.StackRequest;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;
import com.meshnetics.gb.type.impl.zcl.UInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACGetRequest extends StackRequest implements MACCommand, MACReplyAwaited {
    public static final byte CODE = 0x4d;

    private final UInt pibAttribute;

    public MACGetRequest(int pibAttribute) {
        this((byte) (pibAttribute & 0xff));
    }

    public MACGetRequest(byte pibAttribute) {
        this(new Byte(pibAttribute));
    }

    public MACGetRequest(Byte pibAttribute) {
        this((UInt) TypeUInt.UINT_8.fromObject(pibAttribute));
    }

    public MACGetRequest(UInt pibAttribute) {
        super("macGetRequest", StackEventType.LME_TYPE);
        if (pibAttribute == null) {
            throw new NullPointerException("pibAttribute is null");
        }
        this.pibAttribute = pibAttribute.convert(TypeUInt.UINT_8);
    }

    public UInt getPibAttributeType() {
        return pibAttribute;
    }

    public Byte getPibAttribute() {
        return (Byte) pibAttribute.toObject();
    }

    public byte getCode() {
        return CODE;
    }

    public int getSize(RadioMACStackLayer layer) {
        return 1;
    }

    public void serialize(ByteBuffer bb, RadioMACStackLayer layer) {
        pibAttribute.serialize(bb);
    }

    public String toString() {
        return getName() + '(' + pibAttribute + ')';
    }
}
